<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/28 0028
 * Time: 13:56
 */
namespace app\api\model;
use think\Cache;
use think\Db;
use think\Request;

class LuckModel
{
    /**抽奖
     * @param Request $request
     * @return array
     */
    public static function getLuck(Request $request){
        $token = $request->param('token',null);
        //获取token
        if(!$token){
            return ['code'=>408,'msg'=>'请先登录'];
        }
        //检测token是否合法
        if(!$user = UserModel::istoken($token)){
            return ['code'=>402,'msg'=>'账号不存在'];
        }
        $resu = Db::connect();
        //查看今天抽了多少次
        if(!Cache::get($user['openid'].'coujiang')){
            //一次都没抽
            $time = (strtotime(date('Y-m-d',time()))+86400)-time();
            $answer_mangement = $resu->name('answer_mangement')->where('id',1)->find();
            Cache::set($user['openid'].'coujiang',$answer_mangement['frequency']+1,$time);
        }
        //抽奖完毕了
        if(Cache::get($user['openid'].'coujiang')==1){
            return ['code'=>405,'msg'=>'你今天抽奖的次数已用完'];
        }
        //获取抽奖奖品
        $luck = $resu->name('luck_draw')->group('prize_name')->order('Lottery_probability asc')->select();
        //将谢谢参与拿出来开始计算概率
        $max = $luck[count($luck)-1];
        $num = mt_rand(1,100);
        if($num<=$max['Lottery_probability']){
            //没有中奖
            Cache::dec($user['openid'].'coujiang');
            if(mt_rand(1,2)==2){
                $luck[count($luck)-1]['id'] = $luck[count($luck)-1]['id']+4;
            }
            return ['code'=>200,'data'=>$luck[count($luck)-1],'user'=>$user];
        }
        unset($luck[count($luck)-1]);
        //中奖了
        foreach ($luck as $value){
            $num = mt_rand(1,100);
            if($num<=$value['Lottery_probability']){
                Cache::dec($user['openid'].'coujiang');
                if(mt_rand(1,2)){
                    $value['id']= $value['id']+4;
                }
                $user['total_integral'] = (int)$user['total_integral']+(int)trim(trim($value['prize_name'],'+'),'积分');
                $resu->name('p_management')->where('id',$user['id'])->update(['total_integral'=>$user['total_integral']]);
                return ['code'=>200,'data'=>$value,'user'=>$user,'fenshu'=>(int)trim(trim($value['prize_name'],'+'),'积分')];
            }
        }
        //如果前面的大奖都没中，强制中尾奖
        Cache::dec($user['openid'].'coujiang');
        if(mt_rand(1,2)){
            $luck[count($luck)-1]['id']= $luck[count($luck)-1]['id']+4;
        }
        $user['total_integral'] = (int)$user['total_integral']+(int)trim(trim($luck[count($luck)-1]['prize_name'],'+'),'积分');
        $resu->name('p_management')->where('id',$user['id'])->update(['total_integral'=>$user['total_integral']]);
        return ['code'=>200,'data'=>$luck[count($luck)-1],'user'=>$user,'fenshu'=>(int)trim(trim($luck[count($luck)-1]['prize_name'],'+'),'积分')];
    }

    /**获取抽奖物品
     * @param Request $request
     * @return array
     */
    public static function get_luck(Request $request){
        $token = $request->param('token',null);
        if(!$token){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user = UserModel::istoken($token)){
            return ['code'=>403,'msg'=>'请先登录'];
        }
        $data = Db::name('luck_draw')->select();
        return ['code'=>200,'data'=>$data];
    }

    /**计算错误次数
     * @param Request $request
     * @return array
     */
    public static function fail(Request $request){
        $token = $request->param('token',null);
        if(!$token){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user = UserModel::istoken($token)){
            return ['code'=>403,'msg'=>'请登录'];
        }
        $openid = $user['openid'];
        if($num = Cache::get($openid.'shibai')){
            if($num==1){
                return ['code'=>406,'msg'=>'今天次数已用完'];
            }
            Cache::dec($openid.'shibai');
            if(Cache::get($openid.'shibai')==1){
                return ['code'=>406,'msg'=>'今天次数已用完'];
            }else{
                return ['code'=>200,'data'=>1];
            }
        }else{
            $time = (strtotime(date('Ymd',time()))+86400)-time();
            Cache::set($openid.'shibai',3,$time);
            return ['code'=>200,'data'=>2];
        }
    }

    /**检测当前答错次数
     * @param Request $request
     * @return array
     */

    public static function newfail(Request $request){
        $token = $request->param('token',null);
        if(!$token){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user = UserModel::istoken($token)){
            return ['code'=>403,'msg'=>'请登录'];
        }
        $openid = $user['openid'];
        $num = Cache::get($openid.'shibai');
        if($num==1){
            return ['code'=>405,'msg'=>'今天的次数已用完'];
        }else{
            return ['code'=>200,'msg'=>'今天还有次数'];
        }
    }
}